package com.yungu.swift.report.base.service.impl;

import com.github.miemiedev.mybatis.paginator.domain.Order;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.mapper.IMapper;
import com.yungu.swift.base.model.PageVo;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.report.base.service.BaseService;
import com.yungu.swift.utils.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author cuixiuyin
 * @date 2019/9/3
 */
public abstract class BaseServiceImpl<T> implements BaseService<T> {

    public abstract IMapper<T> getMapper();

    @Override
    public ResponseData<Map<String, Object>> queryPage(AdminReportParam reportParam) {
        PageBounds pageBounds = new PageBounds(reportParam.getPageNum(), reportParam.getPageSize());
        if (StringUtils.isNotEmpty(reportParam.getOrderBySort())) {
            pageBounds.setOrders(Order.formString(reportParam.getOrderBySort()));
        }
        PageList<T> pageList = this.getMapper().queryPage(MapUtils.convertObjToMap(reportParam), pageBounds);
        PageVo<T> pageVo = new PageVo(reportParam.getPageNum(), reportParam.getPageSize(), pageList.getPaginator().getTotalCount());
        pageVo.setItems(pageList);
        Map<String, Object> objectMap = MapUtils.convertObjToMap(pageVo);
        objectMap.put("startTime", reportParam.getStartTime());
        objectMap.put("endTime", reportParam.getEndTime());
        return ResponseData.buildSuccessResponse(objectMap);
    }

    @Override
    public ResponseData<List<T>> list(AdminReportParam reportParam) {
        return ResponseData.buildSuccessResponse(this.getMapper().list(MapUtils.convertObjToMap(reportParam)));
    }
}
